from django.db import models
from dingxiang.utils.models import BaseModel
from doctors.models import DoctorModel,DepartmentModel,HospitalModel
from users.models import UserModel
from orders.models import OrderModel
# Create your models here.

# 优惠券表
class CouponModel(BaseModel):
    DISCOUNT_TYPE_CHOICES = (
        (1, '减免'),
        (2, '折扣')
    )
    TYPE_CHOICES = (
        (0, '全场通用'),
        (1, '指定科室'),
        (2, '指定医生'),
        (3, '指定医院')
    )
    GET_TYPE_CHOICES = (
        (0, '系统发放'),
        (1, '购买赠送'),
        (2, '活动赠送'),
        (3, '注册赠送')
    )

    discount = models.SmallIntegerField(choices=DISCOUNT_TYPE_CHOICES, verbose_name='优惠类型', default=1)
    coupon_type = models.SmallIntegerField(choices=TYPE_CHOICES, verbose_name='优惠券类型', default=0)
    get_type = models.SmallIntegerField(choices=GET_TYPE_CHOICES, verbose_name='获取方式', default=0)
    total = models.IntegerField(blank=True, null=True, default=100,verbose_name='发放数量')
    has_total = models.IntegerField(blank=True, null=True, default=100, verbose_name='剩余数量')
    start_time = models.DateTimeField(verbose_name='使用时间')
    end_time = models.DateTimeField(verbose_name='过期时间')
    condition = models.IntegerField(blank=True, null=True, default=0, verbose_name='满足优惠券使用的价格条件')
    per_limit = models.IntegerField(default=1, verbose_name='每人限领张数')
    sale = models.CharField(max_length=100, verbose_name='优惠公式', help_text="*表示打折，示例：*0.8表示打八折<br/>-表示满减，示例：-100表示总价上减去100")

    class Meta:
        db_table = "dx_coupons"
        verbose_name = "优惠券"
        verbose_name_plural = verbose_name

# 优惠券和医生关系表
class CouponDoctorRelationModel(BaseModel):
    doctor = models.ForeignKey(to=DoctorModel, on_delete=models.CASCADE, related_name='to_coupons',db_constraint=False ,verbose_name='医生')
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_doctors',db_constraint=False, verbose_name='优惠券')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    class Meta:
        db_table = "dx_coupon_doctor"
        verbose_name = "优惠券和医生关系"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.doctor.name

# 优惠券和科室关系表
class CouponDepartmentRelationModel(BaseModel):
    department = models.ForeignKey(to=DepartmentModel, on_delete=models.CASCADE, related_name='to_coupons',db_constraint=False, verbose_name='科室')
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_departments',db_constraint=False, verbose_name='优惠券')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    class Meta:
        db_table = "dx_coupon_department"
        verbose_name = "优惠券和科室关系"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.department.name

# 优惠券和医院关系表
class CouponHospitalRelationModel(BaseModel):
    hospital = models.ForeignKey(to=HospitalModel, on_delete=models.CASCADE, related_name='to_coupons',db_constraint=False, verbose_name='医院')
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_hospitals',db_constraint=False, verbose_name='优惠券')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    class Meta:
        db_table = "dx_coupon_hospital"
        verbose_name = "优惠券和医院关系"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.hospital.name


# 优惠券管理类
class CouponManage(BaseModel):
    USE_TYPE_CHOICES = (
        (0, '未使用'),
        (1, '已使用'),
        (2, '已过期')
    )
    user = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, related_name='to_coupons',db_constraint=False, verbose_name='用户')
    coupon = models.ForeignKey(to=CouponModel, on_delete=models.CASCADE, related_name='to_users',db_constraint=False, verbose_name='优惠券')
    use_time = models.DateTimeField(blank=True, null=True, verbose_name='使用时间')
    use_status = models.SmallIntegerField(choices=USE_TYPE_CHOICES, verbose_name='使用状态', default=0)
    order = models.ForeignKey(to=OrderModel, on_delete=models.DO_NOTHING, related_name='to_coupons',db_constraint=False,blank=True,null=True,verbose_name='订单')

    class Meta:
        db_table = "dx_coupon_manage"
        verbose_name = "优惠券发放管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.coupon.name}发放和使用日志'